package impor;

import java.io.Serializable;
import java.sql.*;

import dao.BaseDao;
import entity.student;

public class StuImport extends BaseDao{

	//显示年级信息
	public void showGrade() {
		Connection con = super.getConnection();
		String sql = "select * from grade";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("gname");
				System.out.println("班级号：" + id + "开设的 科目：" + name);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			super.closeAll(con, ps, rs);
		}
	}
	
	//指定查询某个班的课程
	public void find(int id) {
		Connection con = super.getConnection();
		String sql = "select id, gname from grade where id=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if(rs.next()) {
				System.out.println("班级号：" + rs.getInt("id") + "开设的 科目：" + rs.getString("gname"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			super.closeAll(con, ps, rs);
		}
	}
	
	//入学报名(学号自增长)
	public int enter(student s) {
		Connection con = super.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		int id = 0;
		String sql = "insert into student (sname,gradeid ,password) values (?,?,?)";
		try {
			ps = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setString(1, s.getSname());
			ps.setInt(2, s.getGradeid());
			ps.setString(3, s.getPass());
			int row = ps.executeUpdate();
			rs = ps.getGeneratedKeys();  //getGeneratedKeys检索由执行此Statement对象而创建的任何自动生成的密钥
			if (rs.next()) {
				id = rs.getInt(1);
				System.out.print("学号" + id);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			super.closeAll(con, ps, rs);
		}
		return id;
	}
	
	//登陆
	public int login(student stu) {
		Connection con = super.getConnection();
		String sql = "select * from student where stuno=? and password=?";
		PreparedStatement ps=null;
		ResultSet rs=null ;
		int temp = 0;
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, stu.getStuno());
			ps.setString(2, stu.getPass());
			rs = ps.executeQuery();
			if(rs.next()) 
				return temp = 1;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			super.closeAll(con, ps, rs);
		}
		
			return temp;
		
	}
	
	//登陆之后增加考试成绩记录
	public int score(int scId,float score,String sbno,int stuno) {
		String sql = "insert into examresult(id,score,sb_id,st_id) values(?,?,?,?)";
		Object[]args = {scId,score,sbno,stuno};
		int row = super.exeUpdate(sql, args);
		return row;
	}

}
